/*
Copyright (c) 2025 HeZongLun
NahidaProject is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

#include <iostream>
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <atomic>
#include <chrono>
#include <unordered_map>
#include <list>
#include <cassert>

namespace NahidaProject {
	class __declspec(dllexport) TimingWheel {
	public:
		using Task = std::function<void()>;
		using Clock = std::chrono::steady_clock;
		using TimePoint = Clock::time_point;

		explicit TimingWheel(size_t wheelSize = 60, size_t resolutionMs = 1000);
		~TimingWheel();
		void Schedule(const Task& task, size_t delayMs);

	private:
		void Run();

		size_t wheelSize;
		size_t resolutionMs;
		std::atomic<bool> stop;
		std::vector<std::list<Task>> wheel;
		size_t currentTick = 0;
		std::mutex mutex;
		std::condition_variable cv;
		std::thread schedulerThread;
	};
}
/*
int main() {
    TimingWheelScheduler scheduler;

    scheduler.schedule([]() {
        std::cout << "Task 1 executed at " << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;
    }, 1000);

    scheduler.schedule([]() {
        std::cout << "Task 2 executed at " << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;
    }, 3000);

    scheduler.schedule([]() {
        std::cout << "Task 3 executed at " << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;
    }, 5000);

    std::this_thread::sleep_for(std::chrono::seconds(6));

    return 0;
}
*/
